﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplicationGDI3._1
{
    public partial class Form1 : Form
    {
        enum EPlayerAction { Idle = 4, MoveLeft = 1, MoveRight = 2, MoveUp = 3, MoveDown = 0};

        EPlayerAction player1 = EPlayerAction.Idle;
        EPlayerAction player2 = EPlayerAction.Idle;
        bool p1 = false, p2 = false, one = true;
        int x1, x2;
        int y1, y2;
        bool win1 = false, win2 = false;

        public Form1()
        {
            InitializeComponent();
            animSprite = new AnimatedSprite("sr1.bmp", 6, 5, false);
            animSprite1 = new AnimatedSprite("sr1.bmp", 6, 5, false);
        }

        AnimatedSprite animSprite;
        AnimatedSprite animSprite1;

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            using (Graphics gr = e.Graphics)
            {
                Pen p = new Pen(Color.DimGray, 5.0f);
                Brush b = new HatchBrush(HatchStyle.SmallConfetti, Color.Aqua);
                Brush b1 = new HatchBrush(HatchStyle.SmallConfetti, Color.Aqua, Color.DarkBlue);
                Font f = new Font("Georgia", 50.0f);

                Random r = new Random();
                if (one == true)
                {
                    x1 = r.Next(15, 350);
                    y1 = r.Next(156, 540);
                    x2 = r.Next(520, 880);
                    //y2 = r.Next(156, 540);
                    y2 = y1;
                    gr.DrawImage(animSprite.Image, new Rectangle(x1, y1, 120, 120), animSprite.GetAnimationFrame(), GraphicsUnit.Pixel);
                    gr.DrawImage(animSprite1.Image, new Rectangle(x2, y2, 120, 120), animSprite.GetAnimationFrame(), GraphicsUnit.Pixel);
                    one = false;
                }

                animSprite.SetLine((int)player1);

                animSprite1.SetLine((int)player2);

                gr.DrawRectangle(p, new Rectangle(0, 0, 502, 150));
                gr.DrawString("Player 1", f, b, new PointF(10, 20));

                gr.DrawRectangle(p, new Rectangle(502, 0, 500, 150));
                gr.DrawString("Player 2", f, b1, new PointF(512, 20));

                p.Color = Color.Gray;
                p.Width = 2;
                Font f1 = new Font("Georgia", 20.0f);
                Brush b2 = new HatchBrush(HatchStyle.Weave, Color.Red);
                gr.DrawRectangle(p, new Rectangle(362, 15, 40, 40));
                gr.DrawRectangle(p, new Rectangle(362, 70, 40, 40));
                gr.DrawRectangle(p, new Rectangle(307, 70, 40, 40));
                gr.DrawRectangle(p, new Rectangle(417, 70, 40, 40));
                gr.DrawString("W", f1, b2, 365, 20);
                gr.DrawString("S", f1, b2, 370, 75);
                gr.DrawString("A", f1, b2, 312, 75);
                gr.DrawString("D", f1, b2, 423, 75);

                gr.DrawRectangle(p, new Rectangle(875, 15, 40, 40));
                gr.DrawRectangle(p, new Rectangle(875, 70, 40, 40));
                gr.DrawRectangle(p, new Rectangle(820, 70, 40, 40));
                gr.DrawRectangle(p, new Rectangle(930, 70, 40, 40));
                gr.DrawString("I", f1, b2, 885, 20);
                gr.DrawString("K", f1, b2, 880, 75);
                gr.DrawString("J", f1, b2, 827, 75);
                gr.DrawString("L", f1, b2, 937, 75);

                Brush b3 = new HatchBrush(HatchStyle.LargeCheckerBoard, Color.WhiteSmoke, Color.Black);
                gr.DrawRectangle(p, new Rectangle(487, 154, 30, 507));
                gr.FillRectangle(b3, new Rectangle(487, 154, 30, 507));

                if (p1 == true)
                {
                    p1 = false;
                    if (player1 == EPlayerAction.MoveRight)
                    {
                        x1 += 5;
                        if (x1 >= 370)
                        {
                            win1 = true;
                        }
                    }
                    if (player1 == EPlayerAction.MoveLeft)
                    {
                        x1 -= 5;
                        if (x1 <= 0)
                        {
                            x1 += 5;
                        }
                    }
                    if (player1 == EPlayerAction.MoveDown)
                    {
                        y1 += 5;
                        if (y1 >= 545)
                        {
                            y1 -= 5;
                        }
                    }
                    if (player1 == EPlayerAction.MoveUp)
                    {
                        y1 -= 5;
                        if (y1 <= 150)
                        {
                            y1 += 5;
                        }
                    }
                }

                if (p2 == true)
                {
                    p2 = false;
                    if (player2 == EPlayerAction.MoveRight)
                    {
                        x2 += 5;
                        if (x2 >= 885)
                        {
                            x2 -= 5;
                        }
                    }
                    if (player2 == EPlayerAction.MoveLeft)
                    {
                        x2 -= 5;
                        if (x2 <= 515)
                        {
                            win2 = true;
                        }
                    }
                    if (player2 == EPlayerAction.MoveDown)
                    {
                        y2 += 5;
                        if (y2 >= 545)
                        {
                            y2 -= 5;
                        }
                    }
                    if (player2 == EPlayerAction.MoveUp)
                    {
                        y2 -= 5;
                        if (y2 <= 150)
                        {
                            y2 += 5;
                        }
                    }
                }
                gr.DrawImage(animSprite1.Image, new Rectangle(x2, y2, 120, 120), animSprite1.GetAnimationFrame(), GraphicsUnit.Pixel);
                gr.DrawImage(animSprite.Image, new Rectangle(x1, y1, 120, 120), animSprite.GetAnimationFrame(), GraphicsUnit.Pixel);

                player1 = EPlayerAction.Idle;
                player2 = EPlayerAction.Idle;

                if (win1 == true)
                {
                    MessageBox.Show("PLAYER 1 WIN!!!");
                    Close();
                }
                if (win2 == true)
                {
                    MessageBox.Show("PLAYER 2 WIN!!!");
                    Close();
                }
            }
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 'a')
            {
                player1 = EPlayerAction.MoveLeft;
                p1 = true;
            }
            if (e.KeyChar == 'd')
            {
                player1 = EPlayerAction.MoveRight;
                p1 = true;
            }
            if (e.KeyChar == 'w')
            {
                player1 = EPlayerAction.MoveUp;
                p1 = true;
            }
            if (e.KeyChar == 's')
            {
                player1 = EPlayerAction.MoveDown;
                p1 = true;
            }

            if (e.KeyChar == 'j')
            {
                player2 = EPlayerAction.MoveLeft;
                p2 = true;
            }
            if (e.KeyChar == 'l')
            {
                player2 = EPlayerAction.MoveRight;
                p2 = true;
            }
            if (e.KeyChar == 'i')
            {
                player2 = EPlayerAction.MoveUp;
                p2 = true;
            }
            if (e.KeyChar == 'k')
            {
                player2 = EPlayerAction.MoveDown;
                p2 = true;
            }

            Invalidate();
        }
    }
}
